package com.ytzl.itrip.controller;

import com.ytzl.itrip.common.DtoUtil;
import com.ytzl.itrip.common.ErrorCode;
import com.ytzl.itrip.dto.Dto;
import com.ytzl.itrip.pojo.ItripUser;
import com.ytzl.itrip.vo.user.ItripUserVO;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.regex.Pattern;

/**
 * Created by sam on 2018/2/7.
 */
@Controller
@RequestMapping("/api")
public class ItripUserController extends BaseController {


    @ApiOperation(value = "用户名验证", httpMethod = "get", produces = "application/json",
            response = Dto.class, notes = "验证是否有这个用户")
    @RequestMapping(value = "/ckusr", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public Dto query(@ApiParam(name = "name", value = "被检查的用户名") @RequestParam String name) {
        try {
            ItripUser itripUser = itripUserService.findByUserName(name);
            if (itripUser != null) {
                //用户已存在
                return DtoUtil.returnFail("用户已存在", ErrorCode.AUTH_USER_ALREADY_EXISTS);
            } else {
                //用户不存在
                return DtoUtil.returnSuccess();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail(e.getMessage(), ErrorCode.AUTH_UNKNOWN);
        }
    }

    @ApiOperation(value = "用户通过邮箱注册", httpMethod = "post", produces = "application/json", response = Dto.class, notes = "通过邮箱禁用用户注册")
    @RequestMapping(value = "/doregister", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Dto doregister(@ApiParam(value = "用户注册表单VO类", name = "itripUserVO") @RequestBody ItripUserVO itripUserVO) {
        try {
            if (!validEmail(itripUserVO.getUserCode()))
                return DtoUtil.returnFail("邮箱格式不正确", ErrorCode.AUTH_ILLEGAL_USERCODE);
            ItripUser itripUser = new ItripUser();
            itripUser.setUserCode(itripUserVO.getUserCode());
//        itripUser.setUserPassword(itripUserVO.getUserPassword());
            itripUser.setUserType(0);
            //未激活
            itripUser.setActivated(0);
            itripUser.setUserName(itripUserVO.getUserName());
            if (null == itripUserService.findByUserName(itripUser.getUserCode())) {
                //密码加密
                itripUser.setUserPassword(md52Value(itripUserVO.getUserPassword()));
                //添加用户
                itripUserService.save(itripUser);
                return DtoUtil.returnSuccess();
            } else
                return DtoUtil.returnFail("用户已存在，注册失败", ErrorCode.AUTH_USER_ALREADY_EXISTS);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail(e.getMessage(), ErrorCode.AUTH_UNKNOWN);
        }
    }

    @ApiOperation(value = "用户邮箱激活", httpMethod = "PUT", produces = "application/json", notes = "通过激活登录账号", response = Dto.class)
    @RequestMapping(value = "activate", method = RequestMethod.PUT, produces = "application/json")
    @ResponseBody
    public Dto activate(@RequestParam @ApiParam(name = "user", value = "邮箱地址") String user,
                        @RequestParam @ApiParam(name = "code", value = "激活码") String code) {
        try {
            if (itripUserService.activate(user, code))
                return DtoUtil.returnSuccess("激活成功");
            else
                return DtoUtil.returnFail("邮箱激活失败，请检查您的激活码", ErrorCode.AUTH_ACTIVATE_FAILED);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail(e.getMessage(), ErrorCode.AUTH_ACTIVATE_FAILED);
        }

    }

    @ApiOperation(value = "用户手机号注册", httpMethod = "POST", produces = "application/json", notes = "通过手机号注册用户", response = Dto.class)
    @RequestMapping(value = "/registerbyphone", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Dto registerbyphone(@RequestBody @ApiParam(value = "用户注册VO类") ItripUserVO itripUserVO) {
        try {
            if (!validPhone(itripUserVO.getUserCode()))
                return DtoUtil.returnFail("请输入正确的手机号码", ErrorCode.AUTH_ILLEGAL_USERCODE);
            ItripUser itripUser = new ItripUser();
            itripUser.setUserCode(itripUserVO.getUserCode());
            itripUser.setUserName(itripUserVO.getUserName());
            itripUser.setUserType(0);
            //未激活
            itripUser.setActivated(0);
            if (itripUserService.findByUserName(itripUser.getUserCode()) == null) {
                //加密密码
                String md5Pass = md52Value(itripUserVO.getUserPassword());
                itripUser.setUserPassword(md5Pass);
                //保存
                itripUserService.saveByPhone(itripUser);
                return DtoUtil.returnSuccess();
            } else {
                return DtoUtil.returnFail("用户已存在", ErrorCode.AUTH_USER_ALREADY_EXISTS);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail(e.getMessage(), ErrorCode.AUTH_UNKNOWN);
        }
    }

    @ApiOperation(value = "用户手机激活", httpMethod = "PUT", produces = "application/json", notes = "通过手机号激活用户", response = Dto.class)
    @RequestMapping(value = "/validatephone", method = RequestMethod.PUT, produces = "application/json")
    @ResponseBody
    public Dto validatephone(@RequestParam @ApiParam(name = "user", value = "注册手机号") String user,
                             @RequestParam @ApiParam(name = "code", value = "验证码") String code) {
        try {
            if (itripUserService.activate(user, code))
                return DtoUtil.returnSuccess();
            else
                return DtoUtil.returnFail("手机号激活失败，请检查验证码输入是否正确", ErrorCode.AUTH_ACTIVATE_FAILED);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail(e.getMessage(), ErrorCode.AUTH_ACTIVATE_FAILED);
        }
    }
}
